package com.xxxx.yygh.hosp.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.xxxx.yygh.cmnclient.DictFeignClient;
import com.xxxx.yygh.common.result.Result;
import com.xxxx.yygh.hosp.repository.HospitalRepository;
import com.xxxx.yygh.hosp.service.HospitalService;
import com.xxxx.yygh.model.hosp.Hospital;
import com.xxxx.yygh.vo.hosp.HospitalQueryVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import javax.xml.crypto.Data;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class HospitalServiceImpl implements HospitalService {

    @Autowired
    private HospitalRepository hospitalRepository;
    @Autowired
    private DictFeignClient dictFeignClient;

    @Override
    public void save(Map<String, Object> paramMap) {
        // 把参数map集合转换json字符串 hospital
        String jsonString = JSONObject.toJSONString(paramMap);
        // 再把json转为对象Hospital
        Hospital hospital = JSONObject.parseObject(jsonString, Hospital.class);

        // 判断mongodb里是否存在数据
        String hoscode = hospital.getHoscode();
        Hospital hospitalByHoscode = hospitalRepository.getHospitalByHoscode(hoscode);
        if (hospitalByHoscode != null) {
            hospital.setStatus(hospitalByHoscode.getStatus());
            hospital.setUpdateTime(hospitalByHoscode.getUpdateTime());
            hospital.setIsDeleted(0);
            hospitalRepository.save(hospital);
        } else {
            // 如果不存在，进行添加
            hospital.setStatus(0);
            hospital.setCreateTime(new Date());
            hospital.setUpdateTime(new Date());
            hospital.setIsDeleted(0);
            hospitalRepository.save(hospital);
        }
    }

    // 查询医院信息
    @Override
    public Hospital getByHoscode(String hoscode) {
        Hospital hospital = hospitalRepository.getHospitalByHoscode(hoscode);
        return hospital;
    }

    @Override
    public Page<Hospital> selectHospPage(Integer page, Integer limit, HospitalQueryVo hospitalQueryVo) {
        // 创建pageable对象
        Pageable pageable = PageRequest.of(page-1, limit);
        // 创建条件匹配器
        ExampleMatcher matcher = ExampleMatcher.matching()
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
                .withIgnoreCase(true);
        Hospital hospital = new Hospital();
        BeanUtils.copyProperties(hospitalQueryVo, hospital);
        // 创建对象
        Example<Hospital> example = Example.of(hospital,matcher);
        Page<Hospital> all = hospitalRepository.findAll(example,pageable);

        // 获取查询list集合 遍历进行医院等级封装
        all.getContent().stream().forEach(item -> {
            this.setHospitalHosType(item);
        });
        return all;
    }

    @Override
    public void updateHospStatus(String id, Integer status) {
        Hospital hospital = hospitalRepository.findById(id).get();
        hospital.setStatus(status);
        hospital.setUpdateTime(new Date());
        hospitalRepository.save(hospital);
    }

    // 根据id查医院详情信息
    @Override
    public Map<String,Object> getHospById(String id) {
        Map<String,Object> result = new HashMap<>();
        Hospital hospitalById = hospitalRepository.findById(id).get();
        Hospital hospital = this.setHospitalHosType(hospitalById);
        // 医院基本信息（包含医院等级）
        result.put("hospital", hospital);
        // 单独处理更直观
        result.put("bookingRule", hospital.getBookingRule());
        // 不需要重复返回
        hospital.setBookingRule(null);
        return result;
    }

    @Override
    public String getHospName(String hoscode) {
        Hospital hospitalByHoscode = hospitalRepository.getHospitalByHoscode(hoscode);
        String hosname = hospitalByHoscode.getHosname();
        return hosname;
    }

    @Override
    public List<Hospital> findByHosname(String hosname) {
        List<Hospital> hospitalByHosnameLike = hospitalRepository.findHospitalByHosnameLike(hosname);
        return hospitalByHosnameLike;
    }

    // 根据医院编号获取医院挂号详细信息
    @Override
    public Map<String, Object> item(String hoscode) {
        Map<String,Object> result = new HashMap<>();
        // 医院信息
        Hospital hospitalBycode = this.getByHoscode(hoscode);
        // 根据医院里的编号查看医院详情信息
        Hospital hospital = this.setHospitalHosType(hospitalBycode);
        result.put("hospital",hospital);
        result.put("bookingRule", hospital.getBookingRule());
        return result;

    }

    // 获取查询list集合 遍历医院等级封装
    private Hospital setHospitalHosType(Hospital hosp) {
        // 根据dictCode和value获取医院等级名称
        String hostype = dictFeignClient.getName("Hostype", hosp.getHostype());
        // 查询省 市 地区(value查找)
        String provinceString = dictFeignClient.getName(hosp.getProvinceCode());
        String cityString = dictFeignClient.getName(hosp.getCityCode());
        String districtString = dictFeignClient.getName(hosp.getDistrictCode());

        hosp.getParam().put("fullAddress", provinceString+cityString+districtString);
        hosp.getParam().put("hostypeString", hostype);
        return hosp;
    }

}
